package frog.lazy.framework.http.config;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import frog.lazy.framework.core.base.IBaseEnum;
import frog.lazy.framework.core.tools.DateUtils;
import frog.lazy.framework.http.module.IBaseEnumModule;
import frog.lazy.framework.http.serializer.DoubleSerializer;
import frog.lazy.framework.http.serializer.IBaseEnumSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 日期 格式化处理
 * 可以使用  @JsonFormat(pattern = "yyyy-MM-dd")
 */
@Slf4j
@Configuration
public class SerializerConfig {

    @Bean
    public LocalDateTimeSerializer localDateTimeDeserializer() {
        return new LocalDateTimeSerializer(DateUtils.YYYY_MM_DD_HH_MM_DD);
    }

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer());
    }

    @Bean
    public Converter<String, LocalDate> localDateConverter() {
        return new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String source) {
                if(StrUtil.isNotBlank(source)){
                    return LocalDate.parse(source, DateUtils.YYYY_MM_DD);
                }
                return null;
            }
        };
    }

    @Bean
    public Converter<String, LocalDateTime> localDateTimeConverter() {
        return new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String source) {
                if(StrUtil.isNotBlank(source)) {
                    return LocalDateTime.parse(source, DateUtils.YYYY_MM_DD_HH_MM_DD);
                }
                return null;
            }
        };
    }

    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateUtils.YYYY_MM_DD_HH_MM_DD));
        javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateUtils.YYYY_MM_DD));
        javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateUtils.HH_MM_DD));

        javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateUtils.YYYY_MM_DD_HH_MM_DD));
        javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateUtils.YYYY_MM_DD));
        javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateUtils.HH_MM_DD));


        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(BigDecimal.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Double.class, new DoubleSerializer());
        simpleModule.addSerializer(Double.TYPE, new DoubleSerializer());
        simpleModule.addSerializer(IBaseEnum.class, new IBaseEnumSerializer());
        objectMapper.registerModule(javaTimeModule)
                    .registerModule(simpleModule)
                    .registerModule(new ParameterNamesModule())
                    .registerModule(new IBaseEnumModule())
                    .enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)
                    .enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN)
                    .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // 解决 参数多一个属性报错的问题
        // 空值 返回
        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            @Override
            public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws  IOException {
                jsonGenerator.writeString("");
            }
        });//
        objectMapper.setVisibility(
            objectMapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility( JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        return objectMapper;
    }
}
